package com.qf.controller;

import com.qf.entity.Goods;
import com.qf.entity.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.controller
 * @description: ${TODO}
 * @date 2022/6/23 10:59
 */
@WebServlet(urlPatterns = "/goodsservlet")
public class GoodsServlet extends BaseServlet {
    //1 分页查询
    public String getGoodsList(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //1 接收数据
        //1.1接收分页数据
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        //默认页面和页大小
        int pn=1;
        int ps=8;
        try {
            if(!StringUtils.isEmpty(pageNum)){
                pn= Integer.parseInt(pageNum);
                if(pn<1){
                    pn=1;
                }
            }
            if(!StringUtils.isEmpty(pageSize)){
                ps=Integer.parseInt(pageSize);
                if(ps<1){
                    ps=8;
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        //1.2 接收查询条件
        String typeId = request.getParameter("typeId");
        String name = request.getParameter("name");
        //创建StringBuilder对象
        // select * from tb_goods  and typeId=? and title like ?
        StringBuilder where=new StringBuilder();
        //参数
        List<Object> params=new ArrayList<>();
        if(!StringUtils.isEmpty(typeId)){
            try {
                params.add(Integer.parseInt(typeId));
                where.append("and typeid=? ");
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        if(!StringUtils.isEmpty(name)){
            where.append("and name like ? ");
            params.add("%"+name+"%");
        }
        //解决前面 and问题
        if(where.length()>0){
            //替换and
            where.replace(0, 3, "where");
        }else{
            return "redirect:/index.jsp";//不让查询所有
        }
        System.out.println(where.toString());
        //2 创建业务对象
        GoodsService goodsService=new GoodsServiceImpl();
        try {
            PageBean<Goods> pageBean=goodsService.findByPage(pn,ps,where.toString(),params);
            List<Goods> data = pageBean.getData();
            for (Goods d : data) {
                System.out.println(d.toString());
            }
            //3放入域中
            request.setAttribute("pageBean", pageBean);
            request.setAttribute("typeId",typeId);
            request.setAttribute("name", name);
            //4转发
            return "/goodsList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/index.jsp";
    }
    //2 根据商品ID查询商品
    public String getGoodsById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //获取商品id
        String id = request.getParameter("id");
        //校验
        if(StringUtils.isEmpty(id)){
            return "redirect:/index.jsp";
        }
        //创建业务对象
        GoodsService goodsService=new GoodsServiceImpl();
        //调用方法
        try {
            Goods goods=goodsService.findById(Integer.parseInt(id));
            request.setAttribute("goods",goods);
            return "/goodsDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/index.jsp";
    }
}
